package com.oocl.crm.dao.impl;
import com.oocl.crm.dao.CustomerDao;
import com.oocl.crm.db.DoubleLinkList;
import com.oocl.crm.db.Node;
import com.oocl.crm.pojo.Customer;

public class CustomerDaoImpl implements CustomerDao
{
	public DoubleLinkList dll;
	
	public CustomerDaoImpl()
	{
		dll = new DoubleLinkList();
		Init();
	}
	private void Init()
	{
		int len=10;
		Customer[] customers=new Customer[len];
		for(int i=1;i<=len;i++)
		{
			if(i%2==1)
			{
				customers[i-1]=new Customer("00"+i, "data"+i, "Male");
			}
			else
			{
				if(i>=10)
				{
					customers[i-1]=new Customer("0"+i, "data"+i, "Female");
				}
				else
				{
					customers[i-1]=new Customer("00"+i, "data"+i, "Female");
				}
			}
			Node node=new Node(customers[i-1]);
			dll.insertLast(node);
		}
	}
	
	public void displayAll()
	{
		System.out.println("All Customers Information: ");
		Node node = dll.first;
		
		System.out.print(node.data + "\n");
		node = node.right;
		while(node != null)
		{
			System.out.print(node.data+"\n");
			node = node.right;
		}
	}
	//L id:010
	public Customer search(String id)
	{
		Node cur = dll.first;
		while(cur != null)
		{
			if(((Customer) cur.data).getId().equals(id))
			{
				return  (Customer) cur.data;
			}
			cur = cur.right;
		}
		return null;
	}
	//L name:data10,Sex:Male
	public Customer search1(String name, String sex)
	{
		Node cur = dll.first;
		while(cur != null)
		{
			if(((Customer) cur.data).getName().equals(name)&& ((Customer) cur.data).getSex().equals(sex))
			{
				return ((Customer) cur.data);
			}
			cur = cur.right;
		}
		return null;
	}
	//A name:data11,Sex:Female
	public void add(String name,String sex)
	{
		String lastId=((Customer) dll.last.data).getId();
		int id=Integer.parseInt(lastId)+1;
		Customer customer;
		if(id>=10)
		{
			customer=new Customer("0"+id, name, sex);
		}
		else
		{
			customer=new Customer("00"+id, name, sex);
		}
		Node node = new Node(customer);
		dll.insertLast(node);
	}
	
	public void delete(String id) throws Exception
	{
		Customer data=new Customer(id, "", "");
		delete(data);
	}
	public Customer delete(Customer customer) throws Exception
	{
		if(dll.first == null)
			throw new Exception("empty!");
		Node cur = dll.first;
		while(cur != null){
			if(((Customer)cur.data).equals(customer))
			{
				if(cur == dll.last)
					dll.last = cur.left;
				else
					cur.right.left = cur.left;
				if(cur == dll.first)
					dll.first = cur.right;
				else
					cur.left.right = cur.right;

				dll.size--;
				return (Customer) cur.data;
			}
			cur = cur.right;
		}
		return null;
	}
	//U 010 name:Neil
	public void change(String id, String name)
	{
		Customer customer=search(id);
		customer.setName(name);
	}
	//U 010 name:Neil,phone:yyy
	public void change(String id, String name, String phone)
	{
		Customer customer=search(id);
		customer.setName(name);
		customer.setPhone(phone);
	}
	
	private void swap(Node a, Node b)
  	{
  		Customer temp=(Customer) a.data;
  		a.data=b.data;
  		b.data=temp;
  	}
  	public void BubbleSortId()
  	{
  		if(dll.first == null||dll.first.right == null)
  			return;

  		Node end=dll.last;
  		Node start=dll.first;
  		while(end!=start)
  		{
  			Node a=start;
  			Node b=a.right;
  			while(a!=end)
  			{
  				if(((Customer) a.data).compareTo((Customer) b.data)>0)
  					swap(a,b);
  				a=a.right;
  				b=a.right;
  			}
  			end=end.left;
  		}
  	}
  	public void BubbleSortName()
  	{
  		if(dll.first == null||dll.first.right == null)
  			return;

  		Node end=dll.last;
  		Node start=dll.first;
  		while(end!=start)
  		{
  			Node a=start;
  			Node b=a.right;
  			while(a!=end)
  			{
  				if(((Customer) a.data).compareName((Customer) b.data)>0)
  					swap(a,b);
  				a=a.right;
  				b=a.right;
  			}
  			end=end.left;
  		}
  	}
  	
}


